home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Examples / AppKit / ColorTest / Controller.m < prev    next >
Encoding:
Text File  |  1992-06-17  |  7.1 KB  |  267 lines

  1. // You may freely copy, distribute and reuse the code in this example.
  2. // NeXT disclaims any warranty of any kind, expressed or implied,
  3. // as to its fitness for any particular use.
  4.  
  5. // Controller and ColorTest.app written by Keith Bernstein.
  6.  
  7. #import <appkit/appkit.h>
  8. #import "Controller.h"
  9. #import "colorAsAscii.h"
  10.  
  11. #define ATTACH_LIST1   0
  12. #define ATTACH_LIST2   1
  13. #define ATTACH_LIST3   2
  14. #define DETACH_LIST1   3
  15. #define DETACH_LIST2   4
  16. #define DETACH_LIST3   5
  17.  
  18. #define INSERT_BEFORE_WHEELMODE               0
  19. #define INSERT_BEFORE_SLIDERMODE              1
  20. #define INSERT_BEFORE_CUSTOM_PALETTEMODE      2
  21. #define INSERT_BEFORE_LISTMODE                3
  22. #define INSERT_AFTER_LISTMODE                 4
  23.  
  24.  
  25. float insertionPoint = 0.0;
  26.  
  27. @implementation Controller
  28.  
  29. - useAccessoryView:sender {
  30.     NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
  31.     
  32.     if (colorPanel) {
  33.         [colorPanel setAccessoryView:[accessoryViewSwitch state] ? accessoryView : nil];
  34.     } 
  35.     return self;
  36. }
  37.  
  38. - attachMatrixClicked:sender
  39. {
  40.     char appWrapperDir[MAXPATHLEN+1];
  41.     NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
  42.     
  43.     if (!attachedList1) {
  44.         strcpy(appWrapperDir, [[NXBundle mainBundle] directory]);
  45.     strcat(appWrapperDir, "/");
  46.     strcat(appWrapperDir, "attach1.clr");
  47.         attachedList1 = [[NXColorList allocFromZone:[self zone]]
  48.                                             initWithName:"attach1" 
  49.          fromFile:appWrapperDir];
  50.         strcpy(appWrapperDir, [[NXBundle mainBundle] directory]);
  51.     strcat(appWrapperDir, "/");
  52.     strcat(appWrapperDir, "attach2.clr");
  53.         attachedList2 = [[NXColorList allocFromZone:[self zone]]
  54.                                             initWithName:"attach2" 
  55.      fromFile:appWrapperDir];
  56.         strcpy(appWrapperDir, [[NXBundle mainBundle] directory]);
  57.     strcat(appWrapperDir, "/");
  58.     strcat(appWrapperDir, "attach3.clr");
  59.         attachedList3 = [[NXColorList allocFromZone:[self zone]]
  60.                                             initWithName:"attach3" 
  61.          fromFile:appWrapperDir];
  62.     }
  63.     switch([[sender selectedCell] tag]) {
  64.         case ATTACH_LIST1:
  65.         [colorPanel attachColorList:attachedList1];
  66.         [addRedButton setEnabled:YES];
  67.         break;
  68.         case ATTACH_LIST2:
  69.         [colorPanel attachColorList:attachedList2];
  70.         break;
  71.         case ATTACH_LIST3:
  72.         [colorPanel attachColorList:attachedList3];
  73.         break;
  74.         case DETACH_LIST1:
  75.         [colorPanel detachColorList:attachedList1];
  76.         [addRedButton setEnabled:NO];
  77.         break;
  78.         case DETACH_LIST2:
  79.         [colorPanel detachColorList:attachedList2];
  80.         break;
  81.         case DETACH_LIST3:
  82.         [colorPanel detachColorList:attachedList3];
  83.         break;
  84.     }
  85.     return self;
  86. }
  87.  
  88. // This method demonstrates how to add a color to an attached ColorList...
  89. - addRed:sender
  90. {
  91.     char nameBuf[30];
  92.     static int redCount = 1;
  93.     
  94.     sprintf(nameBuf, "New Red%d", redCount++);
  95.     [attachedList1 setColorNamed:nameBuf color:NX_COLORRED];
  96.     
  97.     // This method will update the currently viewed ColorList.
  98.     [[NXColorPanel sharedInstance:NO] updateCustomColorList];
  99.  
  100.     return(self);
  101. }
  102.  
  103. - insertCustomPicker:sender
  104. {
  105.     switch ([[sender selectedCell] tag]) {
  106.         case INSERT_BEFORE_WHEELMODE:
  107.         insertionPoint = NX_WHEEL_INSERTION - 0.001;
  108.         break;
  109.         case INSERT_BEFORE_SLIDERMODE:
  110.         insertionPoint = NX_SLIDERS_INSERTION - 0.001;
  111.         break;
  112.         case INSERT_BEFORE_CUSTOM_PALETTEMODE:
  113.         insertionPoint = NX_CUSTOMPALETTE_INSERTION - 0.001;
  114.         break;
  115.         case INSERT_BEFORE_LISTMODE:
  116.         insertionPoint = NX_LIST_INSERTION - 0.001;
  117.         break;
  118.         case INSERT_AFTER_LISTMODE:
  119.         insertionPoint = NX_LIST_INSERTION + 0.001;
  120.         break;
  121.     }
  122.     return self;
  123. }
  124.  
  125. - colorPanelOpacityOnOff:sender
  126. {
  127.     int selectedCellTag = [[sender selectedCell] tag];
  128.     NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
  129.     
  130.     if (colorPanel = [NXColorPanel sharedInstance:NO]) {
  131.     [colorPanel setShowAlpha:selectedCellTag];
  132.     [appAlphaMatrix selectCellWithTag:selectedCellTag];
  133.     } else {
  134.         // If ColorPanel is not instantiated, "setImportAlpha" should be
  135.     // used.  In fact, usually "setImportAlpha" is all that is ever
  136.     // really needed.
  137.         if ([NXApp doesImportAlpha]) {
  138.         [colorPanelAlphaMatrix selectCellWithTag:1];
  139.     } else {
  140.         [colorPanelAlphaMatrix selectCellWithTag:0];
  141.     }
  142.     }
  143.     return self;
  144. }
  145.  
  146. - appOpacityOnOff:sender
  147. {
  148.     int selectedCellTag = [[sender selectedCell] tag];
  149.     
  150.     [NXApp setImportAlpha:selectedCellTag];
  151.     [colorPanelAlphaMatrix selectCellWithTag:selectedCellTag];
  152.     return self;
  153. }
  154.  
  155. - continuousOnOff:sender
  156. {
  157.     [[NXColorPanel sharedInstance:NO] setContinuous:[[sender selectedCell] tag]];
  158.     return self;
  159. }
  160.  
  161. - isContinuous:sender
  162. {
  163.     BOOL continuous = YES;
  164.     NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
  165.     
  166.     if (colorPanel) {
  167.     continuous = [colorPanel isContinuous];
  168.     } 
  169.     [continuousSwitch setState:continuous];
  170.     return self;
  171. }
  172.  
  173. - showCurrColor:sender
  174. {
  175.     NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
  176.     
  177.     if (colorPanel) {
  178.         [sampleWell setColor:[colorPanel color]];
  179.     }
  180.     return self;
  181. }
  182.  
  183. - alphaValue:sender
  184. {
  185.     NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
  186.     
  187.     if (colorPanel) [alphaText setFloatValue:[colorPanel alpha]];
  188.     return self;
  189. }
  190.  
  191. - queryMode:sender
  192. {
  193.     NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
  194.     
  195.     if (colorPanel) [currModeText setIntValue:[colorPanel mode]];
  196.  
  197.     return self;
  198. }
  199.  
  200. - modeMatrixClicked:sender
  201. {
  202.     NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
  203.     
  204.     if (colorPanel) {
  205.         [colorPanel setMode:[[sender selectedCell] tag]];
  206.     } else {
  207.         [NXColorPanel setPickerMode:[[sender selectedCell] tag]];
  208.     }
  209.     return self;
  210. }
  211.  
  212. - maskMatrixClicked:sender 
  213. {
  214.     int pickerMask = 0;
  215.     int i;
  216.     ButtonCell *aCell;
  217.     
  218.     for (i = [sender cellCount] - 1; i >=0; --i) {
  219.         aCell = [sender cellAt:i :0];
  220.     if ([aCell state]) {
  221.             pickerMask |= [aCell tag];
  222.     }
  223.     }
  224.     [NXColorPanel setPickerMask:pickerMask];
  225.    
  226.     return self;
  227. }
  228.  
  229. - showColorPanel:sender
  230. {
  231.     NXColorPanel *colorPanel = [NXColorPanel sharedInstance:YES];
  232.     if ([accessoryViewSwitch state]) {
  233.         [colorPanel setAccessoryView:accessoryView];
  234.     }
  235.     [colorPanel setContinuous:[[continuousOnOffMatrix selectedCell] tag]];
  236.     [NXApp orderFrontColorPanel:NXApp];
  237.     return self;
  238. }
  239.  
  240. // This method gets called when the .nib is being loaded. The argument is
  241. // a pointer to the main color well in the window. If we've stored
  242. // a color away from the previous invocation of the app, we set the color
  243. // well color. Otherwise we leave the default value (determined in IB)
  244. // alone. The color is saved out to the defaults whenever the app terminates.
  245. // (We could also write it out everytime it changes...)
  246.  
  247. - setTheWell:anObject
  248. {
  249.     NXColor color;
  250.  
  251.     theWell = anObject;    // First set the outlet
  252.     
  253.     if (readColorFromDefaults ("Color", &color)) {
  254.     [theWell setColor:color];
  255.     }        
  256.  
  257.     return self;
  258. }
  259.  
  260. - appWillTerminate:sender
  261. {
  262.     writeColorToDefaults ([theWell color], "Color");
  263.     return self;
  264. }
  265.  
  266. @end
  267.